package com.ruoyi.mateproduction.service.impl;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.mateproduction.domain.MateCleanRecord;
import com.ruoyi.mateproduction.domain.MateCleanRecordModel;
import com.ruoyi.mateproduction.domain.MtCourseCheck;
import com.ruoyi.mateproduction.mapper.MateCleanRecordMapper;
import com.ruoyi.mateproduction.mapper.MateCourseMapper;
import com.ruoyi.mateproduction.service.MateCleanRecordService;
import com.ruoyi.mtAudit.domain.MtAudit;
import com.ruoyi.mtAudit.mapper.MtAuditMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author
 * @Version 1.0
 */
@Service
public class MateCleanRecordServiceImpl implements MateCleanRecordService {

    @Autowired
    MateCleanRecordMapper mateCleanRecordMapper;

    @Autowired
    MtAuditMapper mtAuditMapper;

    @Autowired
    MateCourseMapper mateCourseMapper;

    @Override
    public int addMateCleanRecord(MateCleanRecord mateCleanRecord) {
        int num = 0;
        Map<String, Object> resMap = new HashMap<>();
        try {
            num = mateCleanRecordMapper.addMateCleanRecord(mateCleanRecord);
            if (mateCleanRecord.getMateCleanRecordModels() != null) {
                for (MateCleanRecordModel mateCleanRecordModel : mateCleanRecord.getMateCleanRecordModels()) {
                    mateCleanRecordModel.setDeptId(SecurityUtils.getDeptId());
                    mateCleanRecordModel.setCleanId(mateCleanRecord.getCleanId());
                    mateCleanRecordMapper.addMateCleanRecordModel(mateCleanRecordModel);
                }

                MtAudit mtAudit = new MtAudit();
                mtAudit.setCompanyId(SecurityUtils.getDeptId());
                mtAudit.setOrderId(mateCleanRecord.getCleanId());
                mtAudit.setZjName(mateCleanRecord.getCheckName());
                mtAudit.setOrderType("12");
                mtAudit.setOrderNum(mateCleanRecord.getCleanOrderNum());
                mtAudit.setAuditStatus("0");
                mtAudit.setAuditBy(SecurityUtils.getLoginUser().getNickName());
                mtAuditMapper.insertMtAudit(mtAudit);
                MtCourseCheck mtCourseCheck = new MtCourseCheck();
                mtCourseCheck.setMtCourseId(mateCleanRecord.getMtCourseId());
                mtCourseCheck.setIsCreateCleaning("1");
                mateCourseMapper.updateCourse(mtCourseCheck);

            }

        } catch (Exception e) {
            resMap.put("orderId", mateCleanRecord.getCleanId());
            resMap.put("orderType", "12");
            mtAuditMapper.deleteMtAuditById(resMap);
            mateCleanRecordMapper.removeCleanRecord(mateCleanRecord.getCleanId());
            mateCleanRecordMapper.removeCleanRecordModel(mateCleanRecord.getCleanId());
            MtCourseCheck mtCourseCheck = new MtCourseCheck();
            mtCourseCheck.setMtCourseId(mateCleanRecord.getMtCourseId());
            mtCourseCheck.setIsCreateCleaning("0");
            mateCourseMapper.updateCourse(mtCourseCheck);
        }
        return num;
    }

    @Override
    public List<MateCleanRecord> cleanRecordList(MateCleanRecord mateCleanRecord) {
        return mateCleanRecordMapper.cleanRecordList(mateCleanRecord);
    }

    @Override
    public MateCleanRecord selectCleanRecord(Long cleanId) {
        return mateCleanRecordMapper.selectCleanRecord(cleanId);
    }

    @Override
    public void remove(Long cleanId) {

        MateCleanRecord mateCleanRecord = mateCleanRecordMapper.selectCleanRecord(cleanId);
        MtCourseCheck mtCourseCheck = new MtCourseCheck();
        mtCourseCheck.setMtCourseId(mateCleanRecord.getMtCourseId());
        mtCourseCheck.setIsCreateCleaning("0");
        mateCourseMapper.updateCourse(mtCourseCheck);
        mateCleanRecordMapper.removeCleanRecord(cleanId);
        mateCleanRecordMapper.removeCleanRecordModel(cleanId);
        Map<String, Object> resMap = new HashMap<>();
        resMap.put("orderId", mateCleanRecord.getCleanId());
        resMap.put("orderType", "12");
        mtAuditMapper.deleteMtAuditById(resMap);
    }

    @Override
    public int updateMateCleanRecord(MateCleanRecord mateCleanRecord) {
        int num = 0;
        if (mateCleanRecord.getMateCleanRecordModels() != null) {
            mateCleanRecordMapper.removeCleanRecordModel(mateCleanRecord.getCleanId());
            for (MateCleanRecordModel mateCleanRecordModel : mateCleanRecord.getMateCleanRecordModels()) {
                mateCleanRecordModel.setDeptId(SecurityUtils.getDeptId());
                mateCleanRecordModel.setCleanId(mateCleanRecord.getCleanId());
                mateCleanRecordMapper.addMateCleanRecordModel(mateCleanRecordModel);
            }
            num = mateCleanRecordMapper.updateMateCleanRecord(mateCleanRecord);
        }
        return num;
    }


    @Override
    public List<MateCleanRecord> auditCleanRecord(Long deptId) {
        return mateCleanRecordMapper.auditCleanRecord(deptId);
    }
}
